$(document).ready(function(){
    getsalelist('down');

    mui.init({
        pullRefresh: {
            container: '#pullrefresh',
            down: {
                callback: pulldownRefresh
            },
            up: {
                contentrefresh: '正在加载...',
                callback: pullupRefresh
            }
        }
    });

   


})

function goupAll(id,height){
    var ul=$(id);
    var aFirst=ul.children().eq(0);
    $(id).animate({top:height}).animate({"top":0},0,function(){
        var clone=aFirst.clone();
        $(id).append(clone);
        aFirst.remove();
    })
}

function goturnover(){
    location.href = './turnover/index.html';
}

var page = 1;

function pulldownRefresh() {
    page = 1;
    getsalelist('down',function(){
        mui('#pullrefresh').pullRefresh().endPulldownToRefresh(); 
    });
}
/**
 * 上拉加载具体业务实现
 */
function pullupRefresh() {
    page++;
    getsalelist('up',function(data){
        //参数为true代表没有更多数据了。
        mui('#pullrefresh').pullRefresh().endPullupToRefresh((data.length == 0));
    });
}
function getsalelist(orientation,callback){
    var params = {page:page};
    mzpost({
        url:pub_sale_list,
        data:params,
        success:function(res){
            if(res.code == '2000'){
               var data = res.data;
               if(orientation == 'down'){
                $('.mz-list-box').empty();
               }
               createList(data);
               if(callback){
                    callback(data)
               }
            }else{
                mui.toast(res.msg);
            }
        },
        error:function(err){
            if(orientation == 'down'){
                mui('#pullrefresh').pullRefresh().endPulldownToRefresh();
            }else{
                mui('#pullrefresh').pullRefresh().endPullupToRefresh(false);
            }
            mui.toast('获取订单错误');
        }
    })
}

var currentData = [];
function createList(data){
    if(data.length == 0){
        mui.toast('没有更多订单');
        return;
    }
    currentData = data;
    $.each(data,function(i,item){
        var cbtn ='';
        if(item.status == '2'){
            cbtn = '<button onclick="qrsk(this)" type="button" class="mui-btn mz-xs-btn">确认收款</button>';
        }
    $('<div id="'+item.orde_sn+'" class="mui-content">'+
        '<div class="mui-row mui-row-title">'+
            '<div class="mui-col-xs-4 cell">下单会员</div>'+
            '<div class="mui-col-xs-8 cell">'+item.nickname+'</div>'+
        '</div>'+
        '<div class="mui-row">'+
            '<div class="mui-col-xs-4 cell">订单编号</div>'+
            '<div class="mui-col-xs-8 cell">'+item.orde_sn+'<button type="button" onclick="gotodetail(this)" class="mui-btn mz-xs-btn">查看订单</button></div>'+
        '</div>'+
        '<div class="mui-row">'+
            '<div class="mui-col-xs-4 cell">下单时间</div>'+
            '<div class="mui-col-xs-8 cell">'+formateDate(item.pay_time)+'</div>'+
        '</div>'+
        '<div class="mui-row">'+
            '<div class="mui-col-xs-4 cell">付款通知</div>'+
            '<div class="mui-col-xs-8 cell">'+changepaytype(item.pay_type)+ cbtn + '</div>'+
        '</div>'+
        '<div class="mui-row">'+
            '<div class="mui-col-xs-4 cell">提示</div>'+
            '<div class="mui-col-xs-8 cell">'+changestatus(item.status)+'</div>'+
        '</div>'+
    '</div>').appendTo('.mz-list-box');
    })
}
function gotodetail(obj){
    var orde_sn = $(obj).parent().parent().parent().attr('id');
    var order = currentData.find(function(item){
        return item.orde_sn == orde_sn;
    })
    if(order){
        sessionStorage.order = JSON.stringify(order);
        location.href = './detail/index.html';
    }
}
function qrsk(obj){
    var orde_sn = $(obj).parent().parent().parent().attr('id');
    var params = {orde_sn:orde_sn};
    mzpost({
        url:pub_rece_money,
        data:params,
        success:function(res){
            if(res.code == '2000'){
                mui.toast('确认成功');
                $(obj).remove();
            }else{
                mui.toast(res.msg);
            }
        },
        error:function(err){
            mui.toast('确认错误');
        }
    })
}
function changestatus(a){
    if(a == 2){
        return "请尽快收款";
    }else if(a == 3){
        return "请尽快发货";
    }else if(a == 4){
        return "请尽快确认客户收货";
    }else if(a == 5){
        return "该笔订单已完成";
    }
}
function changepaytype(a){
    if(a == 1){
        return "微信支付";
    }else if(a == 2){
        return "支付宝支付";
    }
}